package com.basicalg.helper;

import com.basicalg.impls.*;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;

import java.util.ArrayList;
import java.util.List;

public class AlgHelper {
        public static boolean less(Comparable a, Comparable b) {
            return a.compareTo(b) < 0;
        }

        public static void swap(Comparable[] a, int i, int j) {
            Comparable temp  =  a[i];
            a[i] = a[j];
            a[j] = temp;
        }

    public static void swap(int[] a, int i, int j) {
        int temp  =  a[i];
        a[i] = a[j];
        a[j] = temp;
    }
        public static boolean isSorted(Comparable a[]) {
            for (int i=0; i<a.length; i++)
                if (less(a[i], a[i-1])) return false;
            return true;
        }

        public static void printArray(Comparable a[]) {
            for (int i=0; i< a.length; i++)
                System.out.print(a[i]+" ");
            System.out.println();
        }

    public static void printArray(int a[]) {
        for (int j : a) System.out.print(j + " ");
        System.out.println();
    }
        public static double time(String alg, Comparable[] arr) {
            StopWatch timer = new StopWatch();
            if(alg.equals("Insertion")) InsertionSort.sort(arr);
            if(alg.equals("Selection")) SelectionSort.sort(arr);
            if(alg.equals("Shell")) ShellSort.sort(arr);
            if(alg.equals("Quick")) QuickSort.sort(arr);
            if(alg.equals("Merge")) MergeSort.sort(arr);
            if(alg.equals("Heap")) HeapSort.sort(arr);
            return timer.elapsTime();
        }

        public static HBox drawStates(int[] arr, int max, int total, int currIndex, int currMin) {
            List<Rectangle> rectangleList = new ArrayList<>();
            for (int i =0; i<arr.length; i++) {
                Rectangle rectangle = new Rectangle(5, ((double) arr[i] / total)  * 10 * arr[i],i== currIndex || i== currMin ? Color.BLACK: Color.GRAY);
                rectangle.setX((i+1) * 8);
                rectangle.setTranslateY(Math.floor(((double) max / total) * 10 * max) - rectangle.getHeight());
                rectangleList.add(rectangle);
            }

            Pane p = new Pane();
            p.setTranslateX(30.0d);
            p.getChildren().addAll(rectangleList);
            Label labelI = new Label(currIndex+" ");
            labelI.setTranslateY(Math.floor(((double) max / total) * 10 * max) - labelI.getHeight());
            Label labelMin = new Label(currMin+" ");
            labelMin.setTranslateY(Math.floor(((double) max / total) * 10 * max) - labelMin.getHeight());
            HBox hBox = new HBox();
            hBox.getChildren().addAll(labelI,labelMin, p);
            return hBox;
        }

        public static HBox drawIndexes(int len, String labelIStr, String labelJStr) {
            List<Label> labelList = new ArrayList<>();
            for (int i = 0; i < len; i++) {
                Label label = new Label(i+" ");
                label.setTranslateX((i+1) * 8);
                label.setFont(Font.font(10));
                labelList.add(label);
            }

            Pane p  =new Pane();
            p.setTranslateX(30.0d);
            p.getChildren().addAll(labelList);
            Label labelI = new Label(labelIStr);
            Label labelJ = new Label(labelJStr);
            HBox hBox = new HBox();
            hBox.getChildren().addAll(labelI,labelJ, p);

            return hBox;
        }
        public static class TotalAndMax{
            private int max;
            private double total;
            public TotalAndMax(int max, double total) {
                this.total = total;
                this.max = max;
            }

            public double getTotal() {
                return total;
            }

            public int getMax() {
                return max;
            }
        }
        public static TotalAndMax getTotalAndMax(int[] arr) {
            int max = arr[0];
            int total = 0;
            for(int i=0; i<arr.length; i++) {
                if (max < arr[i]) {
                    max = arr[i];
                }
                total+=arr[i];
            }

            return new TotalAndMax(max, total);
        }

        public  static int[] ParserToInt(String[] arr) {
            int[] result = new int[arr.length];
            for(int i =0; i< arr.length; i++) {
                int temp = Integer.parseInt(arr[i]);
                result[i] = temp;
            }

            return result;
        }
}
